package com.ccccat.www.msgs;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ccccat.www.msgs.bean.AuthData;
import com.ccccat.www.msgs.service.WakeLockService;
import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketFactory;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class CreateUserDialog extends Dialog {


    private WsListener wsListener;
    Activity context;

    private Button btn_save;

    public EditText text_name;

    public EditText text_gs;


    private View.OnClickListener mClickListener;

    public CreateUserDialog(Activity context) {
        super(context);
        this.context = context;
    }

    public CreateUserDialog(Activity context, int theme, View.OnClickListener clickListener) {
        super(context, theme);
        this.context = context;
        this.mClickListener = clickListener;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
// 指定布局
        this.setContentView(R.layout.dialog);

        text_name = (EditText) findViewById(R.id.text_name);
        text_gs = (EditText) findViewById(R.id.text_gs);

        Window dialogWindow = this.getWindow();

        WindowManager m = context.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.8
        dialogWindow.setAttributes(p);
        btn_save = (Button) findViewById(R.id.btn_save_pop);


        SharedPreferences sharedPreferences = context.getSharedPreferences("data", Context.MODE_PRIVATE);
        String text_names = sharedPreferences.getString("text_name", "手机1");
        Integer text_gss = sharedPreferences.getInt("text_gs", 1);
        text_name.setText(text_names);
        text_gs.setText(text_gss + "");


        final CreateUserDialog createUserDialog = this;

        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (text_name.getText().toString().trim() != null && text_name.getText().toString().trim().length() > 0 && text_gs.getText().toString().trim() != null && text_gs.getText().toString().trim().length() > 0) {
                    SharedPreferences sharedPreferences = context.getSharedPreferences("data", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("text_name", text_name.getText().toString().trim());
                    editor.putInt("text_gs", Integer.parseInt(text_gs.getText().toString().trim()));
                    editor.commit();

                    ConstData.AUTH_DATA.setName(text_name.getText().toString().trim());
                    ConstData.AUTH_DATA.setReadNum(Integer.parseInt(text_gs.getText().toString().trim()));

                    //开启服务
                    Intent intent=new Intent(context,WakeLockService.class);
                    intent.setAction("com.servicestart");
                    context.startService(intent);

                } else {
                    Toast.makeText(context, "请完善信息", Toast.LENGTH_LONG).show();
                }
                createUserDialog.dismiss();

            }
        });
        this.setCancelable(true);
    }


}